package ass.web;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ass.beans.CinemaBean;
import ass.beans.MovieBean;
import ass.dao.support.CinemaDAOImpl;
import ass.dao.support.MovieDAOImpl;

/**
 * Servlet implementation class AddMovieServlet
 */
public class AddMovieServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddMovieServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		
		
//		MOVIE_ID INTEGER NOT NULL,
//		MOVIE_TITLE VARCHAR(200),
//		MOVIE_SYNOPSIS VARCHAR(4000),
//		MOVIE_FROM_DATE DATE,
//		MOVIE_TO_DATE DATE,
//		MOVIE_POSTER BINARY(2147483647),
//		MOVIE_GENRE VARCHAR(100),
//		MOVIE_DIRECTOR VARCHAR(100),
//		MOVIE_AGE VARCHAR(3),
//		MOVIE_ACTOR VARCHAR(100),
//		MOVIE_ACTOR2 VARCHAR(100),
//		MOVIE_SHOWING_TIME FLOAT(17),
//		MOVIE_RATING INTEGER,
		
		String startday = (String) request.getParameter("startdayselect");
		String startmonth = (String) request.getParameter("startmonthselect");
		String startyear =  (String) request.getParameter("startyearselect");
		String endday = (String) request.getParameter("enddayselect");
		String endmonth = (String) request.getParameter("endmonthselect");
		String endyear = (String) request.getParameter("endyearselect");
		Date startDate = null;
		Date endDate = null;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		
        try {
			startDate = formatter.parse(startyear+"-"+startmonth+"-"+startday);
			endDate = formatter.parse(endyear+"-"+endmonth+"-"+endday);
		} catch (ParseException e) {
			
			e.printStackTrace();
		}
		
		
		String title = (String) request.getParameter("title");
		String synopsis = (String)request.getParameter("synopsis");
		
		java.sql.Date fromdate = new java.sql.Date(startDate.getTime());
		java.sql.Date todate = new java.sql.Date(endDate.getTime());
		
		
		
//				                 //we are taking the length of Content type data
//				                 int formDataLength = request.getContentLength();
//				                 byte dataBytes[] = new byte[formDataLength];
//				                 int byteRead = 0;
//				                 int totalBytesRead = 0;
//				                 //this loop converting the uploaded file into byte code
//				                 while (totalBytesRead < formDataLength) {
//				                         byteRead = in.read(dataBytes, totalBytesRead, 
//				 formDataLength);
//				                         totalBytesRead += byteRead;
//				                         }
		
/*
        DataInputStream in = new DataInputStream(request.
			 getInputStream());
        int formDataLength = request.getContentLength();
		byte[] poster = new byte[formDataLength];
		StringBuffer strBuf = new StringBuffer();
		int byteRead = 0;
        int totalBytesRead = 0;
        while (totalBytesRead < formDataLength) {
            byteRead = in.read(poster, totalBytesRead, formDataLength);
            totalBytesRead += byteRead;
        }
		*/
		byte[] poster = null;
		String genre = (String) request.getParameter("genre");
		String director = (String) request.getParameter("director");
		String age = (String) request.getParameter("ageRating");
		String actor = (String) request.getParameter("actor");
		String actor2 = (String) request.getParameter("actor2");
		
	    float showingtime = Float.parseFloat((String) request.getParameter("showtime"));
		int rating = 0;

		MovieBean moviebean = new MovieBean();
		moviebean.setMovie_title(title);
		moviebean.setMovie_synopsis(synopsis);
		moviebean.setMovie_from_date((java.sql.Date)fromdate);
		moviebean.setMovie_to_date((java.sql.Date)todate);
		moviebean.setMovie_poster(poster);
		moviebean.setMovie_genre(genre);
		moviebean.setMovie_director(director);
		moviebean.setMovie_age(age);
		moviebean.setMovie_actor(actor);
		moviebean.setMovie_actor2(actor2);
		moviebean.setMovie_showing_time(showingtime);
		moviebean.setMovie_rating(rating);
		

		MovieDAOImpl movieDAOImpl = new MovieDAOImpl();
		movieDAOImpl.insert(moviebean);

		response.setContentType("text/html");// from response, set content type
		PrintWriter out = response.getWriter();// from response, get output
		out.write (todate.toString());// writer
		out.write("Movie insert succesful");
		//out.close();
		
		String nextJSP = "";
		nextJSP = "/ownerAddMovie.jsp";

		request.setAttribute("movie_inserted",  "<font color='green'>Movie inserted</font>");
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
		dispatcher.forward(request, response);
		
		
	}

}
